from django.contrib import messages
from django.shortcuts import render, redirect

# Create your views here.
from users.forms import RegisterForm


def register(request):
    if request.method == 'GET':
        form = RegisterForm()
    elif request.method == 'POST':
        form = RegisterForm(request.POST)
        if form.is_valid():
            form.save()
            messages.add_message(request, messages.SUCCESS, '恭喜你注册成功！')
            return redirect('blog:index')
    return render(
        request, 'form.html', {'form': form, 'title': '注册'})


def success(request):
    # 在首页展示对应的msg消息内容
    # ?msg=恭喜你，注销成功！
    # http://127.0.0.1:8000/success?msg=恭喜你，注销成功！
    msg = request.GET.get('msg', '恭喜你,操作成功！')
    messages.add_message(request, messages.SUCCESS, msg)
    return redirect('blog:index')
